/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.bancodedados.daoimplemente.cartaocredito;

import camada.bancodedados.conexao.Conexao;
import camada.bancodedados.daoimplemente.contabancaria.ContaBancariaDAOImpl;
import camada.negocio.dao.cartaocredito.ContaCartaoCreditoDAO;
import eloma.classe.cartaocredito.ContaCartaoCredito;
import eloma.classe.contabancaria.ContaBancaria;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Icaro
 */
public class ContaCartaoCreditoDAOImpl implements ContaCartaoCreditoDAO {

    private static Connection con;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    @Override
    public void inserir(ContaCartaoCredito l) throws Exception {

        try {

            con = Conexao.getCon();
            con.setAutoCommit(false);

            final String SQL_SCRIPT = "insert into contas_cartao_credito (idContaBancaria,dataCompra,descricao,parcelas,valorBruto) values (?,?,?,?,?)";

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, l.getContaBancaria().getIdContaBancaria());
            stmt.setDate(2, l.getDataCompra());
            stmt.setString(3, l.getDescricao());
            stmt.setInt(4, l.getParcelas());
            stmt.setDouble(5, l.getValorBruto());

            stmt.executeUpdate();

            con.commit();

        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
    }

    @Override
    public ContaCartaoCredito buscaId(int id) throws Exception {

        try {

            final String SQL_SCRIPT = "select * from contas_cartao_credito where idContasCartaoCredito = ?";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();

            con.commit();

        } catch (SQLException sqle) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }

        return carregarResultadoSimples(rs);
    }

    @Override
    public void atualizar(ContaCartaoCredito l) throws Exception {

        try {

            final String SQL_SCRIPT = "update contas_cartao_credito set idContaBancaria = ?, dataCompra = ?, descricao = ?, parcela = ?, valorBruto = ? where idContasCartaoCredito = ?";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, l.getContaBancaria().getIdContaBancaria());
            stmt.setDate(2, l.getDataCompra());
            stmt.setString(3, l.getDescricao());
            stmt.setInt(4, l.getParcelas());
            stmt.setDouble(5, l.getValorBruto());
            stmt.setInt(6, l.getIdContasCartaoCredito());

            stmt.executeUpdate();

            con.commit();

        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
    }

    @Override
    public void remover(ContaCartaoCredito l) throws Exception {

        try {

            final String SQL_SCRIPT = "delete from contas_cartao_credito where idContasCartaoCredito = ?";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, l.getIdContasCartaoCredito());
            stmt.executeUpdate();

            con.commit();

        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }

    }

    @Override
    public List<ContaCartaoCredito> buscaTodos() throws Exception {

        try {

            String SQL_SCRIPT = "Select * from contas_cartao_credito order by idContasCartaoCredito";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            rs = stmt.executeQuery();

            con.commit();

        } catch (SQLException sqle) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }

        return carregarMultiplosResultados(rs);

    }

    public List<ContaCartaoCredito> carregarMultiplosResultados(ResultSet rs) throws SQLException {
        List<ContaCartaoCredito> resultList = new ArrayList<ContaCartaoCredito>();
        while (rs.next()) {
            ContaCartaoCredito dto = new ContaCartaoCredito();
            carregarVO(dto, rs);
            resultList.add(dto);
        }
        return resultList;
    }

    private ContaCartaoCredito carregarResultadoSimples(ResultSet rs) throws SQLException {
        if (rs.next()) {
            ContaCartaoCredito dto = new ContaCartaoCredito();
            carregarVO(dto, rs);
            return dto;
        } else {
            return null;
        }
    }

    private void carregarVO(ContaCartaoCredito dto, ResultSet rs) throws SQLException {
        try {

            ContaBancariaDAOImpl daoContaBancaria = new ContaBancariaDAOImpl();

            dto.setIdContasCartaoCredito(rs.getInt("idContasCartaoCredito"));

            ContaBancaria c = daoContaBancaria.buscaBancoId(rs.getInt("idContaBancaria"));
            dto.setContaBancaria(c);
            dto.setDataCompra(rs.getDate("dataCompra"));
            dto.setDescricao(rs.getString("descricao"));
            dto.setParcelas(rs.getInt("parcelas"));
            dto.setValorBruto(rs.getDouble("valorBruto"));

        } catch (Exception e) {

        }
    }

}
